package Model;

import Database.AccountDB;
import Entity.Account;
import Manager.Manager;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author CONG
 */
public class LoanRegisterTableModel extends AbstractTableModel {
    private Manager manager;
    private AccountDB dbAccount;
    private ArrayList<Account> listAcc=new ArrayList<Account>();
    public LoanRegisterTableModel(Manager manager) {
        this.manager = manager;
        if(manager.getAccountManager().getSize()>=1){
            for (int i = 0; i < manager.getAccountManager().getSize(); i++) {
                Account acc=manager.getAccountManager().getAccount(i);
                listAcc.add(acc);
            }
        }
    }
    
    @Override
    public int getRowCount() {
       return listAcc.size();
    }

    @Override
    public int getColumnCount() {
        return 7;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        LoanRegisterTableModel model=new LoanRegisterTableModel(manager);
        SimpleDateFormat dateFormat=new SimpleDateFormat("MM/dd/yyyy");
        Account acc=listAcc.get(rowIndex);
        //System.out.println(manager.getAccountManager().getSize());
        int registerNo=acc.getResgisterNo();
        int period=-1;
        int branchCode=-1;
        int accountLoanCode=-1;
        for (int i = 0; i < manager.getAccountDetailsManager().getSize(); i++) {
            if(manager.getAccountDetailsManager().getAccountDetails(i).getRegisterNo()==registerNo){
                period=manager.getAccountDetailsManager().getAccountDetails(i).getPeriod();
                branchCode=manager.getAccountDetailsManager().getAccountDetails(i).getBranchCode();
                accountLoanCode=manager.getAccountDetailsManager().getAccountDetails(i).getAccountLoanCode();
            }
        }
        String branchName="";
        for (int i = 0; i <manager.getBranchManager().getSize(); i++) {
           if(manager.getBranchManager().getBranch(i).getBranchCode()==branchCode){
               branchName=manager.getBranchManager().getBranch(i).getName();
           }
        }
        float loanAmount=0;
        int typeCode=-1;
        for (int i = 0; i < manager.getDurationManager().getSize(); i++) {
          if(manager.getDurationManager().getDuration(i).getAccountLoanCode()==accountLoanCode){
              loanAmount=manager.getDurationManager().getDuration(i).getAmountLoan();
              typeCode=manager.getDurationManager().getDuration(i).getLoanTypeCode();
          }
        }
        String loanType="";
        for (int i = 0; i < manager.getLoanTypeManager().getSize(); i++) {
            if(manager.getLoanTypeManager().getLoanType(i).getLoanTypeCode()==typeCode){
                loanType=manager.getLoanTypeManager().getLoanType(i).getName();
            }
        }
        switch(columnIndex){
            case 0:return acc.getResgisterNo();
            case 1:return dateFormat.format(acc.getRegisterDate());
            case 2:return acc.getName();
            case 3:return loanAmount;
            case 4:return loanType;
            case 5:return period;
            case 6:return branchName;
            default:return null;
        }
    }

    @Override
    public String getColumnName(int column) {
       switch(column){
           case 0:return "ID";
           case 1:return "Birthday";
           case 2:return "Full Name";
           case 3:return "Amount Loan";
           case 4:return "Loan Type";
           case 5:return "Period";
           case 6:return "Branch";
           default:return null;
       }
    }
    public int getSize(){
        return listAcc.size();
    }
    public Account getAccount(int index){
        return listAcc.get(index);
    }
    public void addLoanRegister(Account acc){
        listAcc.add(acc);
        fireTableDataChanged();
    }
}
